const userBox = document.querySelector('#uname');
console.log(userBox );
const pwdBox = document.querySelector('#password');
console.log(pwdBox);
const form = document.querySelector('form');
console.log(form);
const overBox = document.querySelector('.over');

form.addEventListener('submit',e=>{
    e= e || window.event;
    try{e.preventDefault()} catch(err){e.returnValue = false};
    const userBoxV = userBox.value;
    const pwdBoxV = pwdBox.value;
    console.log(pwdBoxV);
    console.log(123);
    if(!userBoxV || !pwdBoxV)alert('账户名和密码不能为空')
   
   const xhr = new XMLHttpRequest();
   xhr.open('post','http://localhost:8888/users/login');
   xhr.onload=()=>{
       let res = JSON.parse(xhr.responseText);
       console.log(res);
       if(res.code !== 1) {
        alert('账号或密码有误，请重新输入')
        userBox.value=''
        pwdBox.value=''
        return
       }
       window.localStorage.setItem('token',res.token);
       window.localStorage.setItem('id',res.user.id);
       window.location.href='./index.html'
   }
   xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
   xhr.send(`username=${userBoxV}&password=${pwdBoxV}`)
})
